package com.linkage.educloud.ah.mvp.presenter;

import android.support.v4.util.ArrayMap;

import com.linkage.educloud.ah.MyApplication;
import com.linkage.educloud.ah.bean.ClazzContact;
import com.linkage.educloud.ah.config.AccountConfig;
import com.linkage.educloud.ah.mvp.IBasicView;
import com.linkage.educloud.ah.mvp.contract.IContactClazzContract;
import com.zzhou.common.library.util.LogUtils;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * <desc>
 * Created by The Moss on 2018/11/20.
 */

public class ContactClazzPresenter extends IContactClazzContract.IPresenter {
    private List<ClazzContact> contacts;
    private ArrayMap<String,Integer> lettes;
    public ContactClazzPresenter(IBasicView iView) {
        super(iView);
        contacts = new ArrayList<>();
    }

    @Override
    public void loadDatasFromLocal(final long clazzId) {
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> o){
                try {
                    //获取教师
                    List<ClazzContact> contacts_t = MyApplication.getInstance().getResovler().getContactsById(AccountConfig.getLoginAccount().getLoginAccount(), clazzId+"", ClazzContact.ContactType.TEACHER);
                    LogUtils.d("teacher num = " +contacts_t.size());
                    if (contacts_t != null && contacts_t.size()>0) {
                        contacts.addAll(contacts_t);
                    }
                    //根据首字母排序 获取家长
                    List<ClazzContact> contacts_p = MyApplication.getInstance().getResovler().getContactsById(AccountConfig.getLoginAccount().getLoginAccount(), clazzId+"", ClazzContact.ContactType.PARENT);
                    LogUtils.d("parent num = " +contacts_p.size());
                    if (contacts_p != null) {
                        perpearContacts(contacts_p);
                    }
                    o.onNext(true);
                } catch (Exception e) {
                    e.printStackTrace();
                    o.onError(new Throwable(e));
                }
            }
        }).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        ((IContactClazzContract.IView)iView).setRecyclerAdapterDatas(contacts,lettes);
                        LogUtils.d("加载数据成功");
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        ((IContactClazzContract.IView)iView).setRecyclerAdapterDatas(contacts,lettes);
                        LogUtils.d("加载数据失败");
                    }
                });
    }
    /*保存每个字母下的联系人在数据中的位置*/
    private void perpearContacts(List<ClazzContact> cons){
        lettes = new ArrayMap<>();
        for (int i = 0; i < cons.size(); i++) {
            ClazzContact contact = cons.get(i);
            if (!lettes.containsKey(contact.getFirstName())) {
                lettes.put(contact.getFirstName(),i + this.contacts.size());
            }
        }
        this.contacts.addAll(cons);
    }
}
